﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Holidays {

	public static class DateHelper {

		public static DateTime[] GetHolidays(Int32 year) {
			return new List<DateTime>().ToArray();
		}

		public static DateTime GetFirstInMonth(Int32 year, Int32 month, DayOfWeek dow) {
			DateTime output = new DateTime(year, month, 1);
			while (output.DayOfWeek != dow) {
				output = output.AddDays(1);
			}

			return output;
		}

		public static DateTime GetLastInMonth(Int32 year, Int32 month, DayOfWeek dow) {
			// Last day of month
			DateTime output = new DateTime(year, month, 1).AddMonths(1).AddDays(-1);
			while (output.DayOfWeek != dow) {
				output = output.AddDays(-1);
			}

			return output;
		}

		/*
		 
function julday(mm, id, iyyy) {
	if (iyyy == 0) {
		alert("Julday: there is no year zero");
		return 0;
	} else {
		if (iyyy < 0) { iyyy += 1; }
		if (mm > 2) {
			jy = iyyy;
			jm = 1.0 * mm + 1;
		} else {
  			jy = iyyy - 1;
  			jm = 1.0 * mm + 13;
		}
		j1 = 1.0 * Math.floor(365.25 *jy);
		j2 = 1.0 * j1 + Math.floor(30.6001 * jm);
		j3 = 1.0 * j2 + 1.0 * id;
		j4 = 1.0 * j3 + 1720995;
		if (id+31*(mm+12*iyyy) >= (15+31*(10+12*1582))) {
			  ja = 1.0 * Math.floor(0.01 * jy);
			jb = 1.0 * Math.floor(0.25 * ja);
			  j5 = 1.0 * j4 + 2;
			j6 = 1.0 * j5 - ja;
			jul = 1.0 * j6 + jb;
		} else {
			jul = j4;
		}
		return jul;
	}
}
var month, day, year;
function caldat(julian) {
	julian = 0 + julian;
	if (julian >= 2299161) {
		jalpha = Math.floor((1.0 * (julian - 1867216)-0.25)/36524.25);
		ja = 1 * julian + 1 + jalpha - Math.floor(0.25 * jalpha);
	} else {
		ja = julian;
	}
	jb = ja + 1524;
	jc = Math.floor(6680.0 + ((jb - 2439870)-122.1)/365.25);
	jd = 365 * jc + Math.floor(0.25 * jc);
	je = Math.floor((jb - jd)/ 30.6001);
	day = jb - jd - Math.floor(30.6001 * je);
	month = je - 1;
	if (month > 12) { month -= 12; }
	year = jc - 4715;
	if (month > 2) { --year; }
	if (year <= 0) { --year; }
}
var jd, frac;
function flmoon(n, nph) {
	RAD = 3.14159265/180.0;
	c = n + nph / 4.0;
	t = c / 1236.85;
	t2 = t * t;
	as = 359.2242 + 29.105356 * c;
	am = 306.0253 + 385.816918 * c + 0.010730 * t2;
	jd = 2415020 + 28 * n + 7 * nph;
	xtra = 0.75933 + 1.53058868 * c + ((1.178e-4) - (1.55e-7) * t) * t2;
	if (nph == 0 || nph == 2)
		xtra += (0.1734 - 3.93e-4 * t) * Math.sin(RAD * as) - 0.4068 * Math.sin(RAD * am);
	else if (nph == 1 || nph == 3)
		xtra += (0.1721 - 4.0e-4 * t) * Math.sin(RAD * as) - 0.6280 * Math.sin(RAD * am);
	else
		alert("nph is unknown in flmoon");
	i = (xtra > 0.0 ? Math.floor(xtra) :  Math.ceil(xtra - 1.0));
	jd += i;
	frac = xtra - i;
}

function doit() {
	thisyear=document.forms[0].year.value;
	thismonth=document.forms[0].month.value;
	thisday=document.forms[0].day.value;
	TZ=Math.round(document.forms[0].tz.value * 4) / 4;
	document.forms[0].tz.value = TZ;
	if (document.forms[0].sign.selectedIndex > 0) {
		TZ = -TZ;
	}
	timzon = TZ / 24.0;
	n = Math.floor(12.37 * (thisyear -1900 + ((1.0 * thismonth - 0.5)/12.0)));
	nph = 2;
	j1 = julday(thismonth,thisday,thisyear);
	flmoon(n,nph); 
	n += Math.floor(1.0 * (j1 - jd) / 28.0); cycle = 1;
	for (var iii = 1; iii <= 12; iii++) {
		flmoon(n, nph);
		frac = 24.0 * (frac + timzon);
		if (frac < 0) {
			--jd;
			frac += 24.0;
		}
		if (frac > 12.0) {
			++jd;
			frac -= 12.0;
		} else
			frac += 12.0;
		i1 = Math.floor(frac);
		secs = 3600.0 * (frac - i1);
		i2 = Math.floor(secs / 60.0);
		i3 = Math.floor(secs - 60 * i2 + 0.5);
		caldat(jd);
		theDate = new Date(year,month-1,day,i1,i2,i3);
		if (nph == 2) { 
			document.forms[cycle].field1.value = theDate.toString(); 
		} else if (nph == 3) {
			document.forms[cycle].field2.value = theDate.toString(); 
		} else if (nph == 0) {
			document.forms[cycle].field3.value = theDate.toString(); 
		} else if (nph == 1) {
			document.forms[cycle].field4.value = theDate.toString(); 
		}
		if (nph == 3) {
			nph = 0;
			++n;
		} else
			++nph;
		if (nph == 2) {
			++cycle;
		}
	}
}
today = new Date(); thisyear = today.getYear(); 
thismonth = today.getMonth(); thisday = today.getDate();
if (thisyear < 50) { thisyear += 2000; }
if (thisyear < 100) { thisyear += 1900; }
thismonth += 1;
TZ = Math.round(today.getTimezoneOffset() / 15) / 4;
TZ = -TZ;




document.write("<center><h2>Moon Phase Calculator</h2>" +
	"<h4>Dates and <a href=\"#caveat\">approximate times</a> of the moon's phases</h4>" +
	"<form name=\"form1\" >" +
	"<b><a href = \"#start\">Starting date</a>:</b> Year:<input type=\"text\" name = \"year\" size=4 value=" + thisyear + ">" +
	" Month:<input type=\"text\" name = \"month\" size=2 value=" + thismonth + ">" +
	" Day:<input type=\"text\" name = \"day\" size=2 value=" + thisday + ">" +
	"<br><b><a href=\"#tz\">Local timezone offset</a>:</b> GMT <select name=\"sign\">" +
	"<option " + (TZ >= 0 ? "selected" : "") + ">Plus" +
	"<option " + (TZ <  0 ? "selected" : "") + ">Minus" +
	"</select> <input type=\"text\" name = \"tz\" size=4 value=" + Math.abs(TZ) + ">" +
	"hour(s)<br><br><input type=\"button\" value=\"Recalculate\" onClick=\"doit()\">" +
	"</form></center>");
<!-- -->
		 */
	}
}